package com.example.studentmanager.service.impl;

import com.example.studentmanager.mapper.GradeMapper;
import com.example.studentmanager.model.Grade;
import com.example.studentmanager.model.vo.GradeVo;
import com.example.studentmanager.service.GradeService;
import com.example.studentmanager.utils.BaseDao;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;


@Component
@Service
public class GradeServiceImpl implements GradeService {
    SqlSession sqlSession= new BaseDao().getSession();
    GradeMapper gradeMapper=sqlSession.getMapper(GradeMapper.class);


    @Autowired
    Grade grade;


    @Override
    public void addGrade(Grade grade) {

        gradeMapper.insertGrade(grade);
        sqlSession.commit();  //提交事务
    }

    @Override
    public Grade searchGrade(int studentid, int courseid) {
        grade=gradeMapper.selectGrade(studentid,courseid);
        if(grade!=null)
            return grade;
        else
            throw new RuntimeException("未查询到分数异常");
    }

    @Override
    public void delGrade(int studentid, int courseid, String studentname) {
        gradeMapper.deleteGrade(studentid,courseid,studentname);
        sqlSession.commit();
    }

    @Override
    public void changeGrade(Grade grade) {
        gradeMapper.updateGrade(grade);
        sqlSession.commit();
    }

    @Override
    public List<GradeVo> listGrades(Integer page, Integer rows, String searchCol, String searchValue) {
        Integer start=(page-1)*rows;
        List<GradeVo> grades=gradeMapper.selectAllGrade(start,rows,searchCol,searchValue);
        if(grades!=null)
          return grades;
        else
            throw new RuntimeException("分数未查询到");
    }

    @Override
    public Integer counts(String searchCol, String searchValue) {
        return gradeMapper.counts(searchCol, searchValue);
    }

   /* @Override
    public List<Grade> searchGradeBy(int courseid, int studentid, String coursename, String studentname) {

        List<Grade> grades=gradeMapper.selectGradeBy(courseid,studentid,coursename,studentname);
        return grades;
    }*/
}
